package controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Domain;
import server.PutCache;
import server.Update;
import server.UpdateDomain;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class UpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String name = request.getParameter("name");
		if(name!=null&&Utils.checkDomain(name))
		{
			try {
				ObjectifyService.register(Domain.class);
			} catch (Exception e) {
				// TODO: handle exception
			}
			Objectify objectify = ObjectifyService.begin();
			try {
				Domain domain = objectify.get(new Key<Domain>(Domain.class, name));
				
				if(domain.getDate().getTime()<new Date().getTime()-2*24*60*60*1000)
				{
					new UpdateDomain(domain);
				}
				PutCache.putCacheList(domain.getDomain(), "update",18);
				
				request.setAttribute("domain", domain);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
